# 网址：https://jingji.cctv.com/?spm=C94212.P4YnMod9m2uD.0.0
# 需求数据所在请求：https://news.cctv.com/2019/07/gaiban/cmsdatainterface/page/economy_zixun_1.jsonp?cb=economy_zixun

import requests
import json
from entity.News import News
import time


def getCCTVNews():
    newsList = []
    url = 'https://news.cctv.com/2019/07/gaiban/cmsdatainterface/page/economy_zixun_1.jsonp?cb=economy_zixun'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
    }
    resp = requests.get(url=url, headers=headers)
    resp.encoding = 'utf-8'
    resp = resp.text.strip("economy_zixun(").strip(")")
    data = json.loads(resp)
    for i in data['data']['list']:
        if verifyTime(i['focus_date']):  # 只筛选当天数据
            news = News("央视财经", i['title'], i['url'], i['id'])
            newsList.append(news.__dict__)
    return newsList


# 筛选当天时间数据
def verifyTime(dateStr):
    currentDate = time.strftime('%Y-%m-%d', time.localtime())
    return currentDate in dateStr
